package com.gzhryc.shared_device.oem.micro.queue.work;

import java.util.List;

import com.gzhryc.common.logging.Logger;
import com.gzhryc.shared_device.base.models.OperateInfo;
import com.gzhryc.shared_device.cdcz.code.dao.db.CDCZOrder;
import com.gzhryc.shared_device.cdcz.code.services.CDCZOrderService;
import com.gzhryc.shared_device.oem.code.order.RedisCacheManager;
import com.gzhryc.shared_device.oem.micro.queue.CDCZOrderOperateHelper;

public class CDCZCacheFinishCheckRunnable implements Runnable{

    static Logger log = Logger.getLogger(CDCZCacheFinishCheckRunnable.class);

    OperateInfo operateInfo;

    public CDCZCacheFinishCheckRunnable(OperateInfo operateInfo){
        this.operateInfo = operateInfo;
    }

    @Override
    public void run() {
        try {
            List<CDCZOrder> deviceOrderList = RedisCacheManager.findWorkingCDCZDeviceOrder();
            if(deviceOrderList != null && deviceOrderList.size() > 0) {
                for (CDCZOrder deviceOrder : deviceOrderList) {
                    if (deviceOrder.getEndDate() == null) {
                    	 //从数据库获取设备订单信息，避免缓存脏数据
                        deviceOrder = CDCZOrderService.self(deviceOrder.getCreateDate()).getByTradeNo(deviceOrder.getTradeNo());
                    }

                    try {
                    	CDCZOrderOperateHelper.chekcTimeOrToFinish(deviceOrder,operateInfo);
                    } catch (Exception e) {
                        log.error(e.getMessage(), e);
                    }
                }
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }
}
